#!/bin/bash
# description:　PCS service deploy
# version:	1.0
# author:	zhangyanbin
# contact:	yanbin_zhang@foxitsoftware.com
#
#Init current directory
# Include shared functions (like setting a download server)

. functions.sh

echo_and_log "--> Starting servicelayer.sh"

if [ -z "$SOURCE_ROOT" ]; then
	SOURCE_ROOT="/vagrant"
fi
if [ -z "$HOST_ENV" ]; then
	rm -rf /etc/foxitcloud/main.properties
	rm -rf /etc/foxitcloud/fp.properties
fi

function DeployTomcatApps()
{
	echo_and_log "--> Deploying Java war files to Tomcat"
	sudo rm -rf $TOMCAT_HOME/webapps/*
	# warpkgs=( foxit-filesystem-web foxit-pcs-web foxit-sso foxit-user-center-web foxit-fpforms-acroformsvc foxit-tools-web foxit-apimgt-web foxit-reading-web )
	warpkgs=(foxit-apimgt-web)
	for i in "${warpkgs[@]}"
	do
		echo_and_log "--> Deploying $i war files to Tomcat"
		unzip -q -o /tmp/war/$i.war -d $TOMCAT_HOME/webapps/$i
	done
# The war of plugin is unused for now.
	#VAGRANT_CACHE_DIR=/tmp/vagrant-cache/wget
	#wget -N -P ${VAGRANT_CACHE_DIR} http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz 
	#gunzip -c ${VAGRANT_CACHE_DIR}/GeoLite2-Country.mmdb.gz > $TOMCAT_HOME/webapps/foxit-plugin-web/WEB-INF/classes/data/GeoLite2-Country.mmdb

	#sudo cp -r $FORMSVC_DIR/target/foxit-fpforms-acroformsvc $TOMCAT_HOME/webapps
}


#Consont variable
#FORMSVC_DIR=$SOURCE_ROOT/websvcs/fpforms-acroformsvc/fpforms-acroformsvc
if [ -z "$TOMCAT_HOME" ]; then
  TOMCAT_HOME=/usr/share/tomcat
fi

if [ ! -e /usr/share/tomcat/latest/temp ]; then
	sudo mkdir -p /usr/share/tomcat/latest/temp
fi

if [ ! -e /usr/share/tomcat/latest/temp/v2 ]; then
        sudo mkdir -p /usr/share/tomcat/latest/temp/v2
fi

#  commented out by tingl March 4. #9936
#Deploy java project to tomcat,first move app file to tomcat webapps directory,then add Host to tomcat server.xml file
#DeployTomcatApps

# Needed for Platform v2
#=====note:do this in smx_download bcz platform worker need it when smx_platform smx_worker excuting
# sudo \cp /tmp/sofiles_64/libFormWidgetGen.so /usr/share/tomcat/latest/temp/v2/libFormWidgetGen.so
# sudo \cp /tmp/sofiles_64/libPDF2Image.so /usr/share/tomcat/latest/temp/v2/libPDF2Image.so
# sudo \cp /tmp/sofiles_64/libfpforms_x64.so /usr/share/tomcat/latest/temp/libfpforms_x64.so
#sudo \cp /tmp/sofiles_64/PDFLinearize_x64.so /usr/share/tomcat/latest/temp/PDFLinearize_x64.so
#sudo \cp /tmp/sofiles_64/libPdfEngine_x64.so /usr/share/tomcat/latest/temp/v2/libPdfEngine_x64.so
# TODO: need the "Linearization" SO file

# commented out by tingl March 4. #9936
#Restart tomcat let service app active.for some reason,we can't use 'service tomcat restart',if stop failed,start will failed too.
#echo_and_log "--> Restart tomcat, shell execute very quickly,but it may takes minutes for tomcat to start completely"
#sudo service tomcat stop
#sudo service tomcat start

#Deploy to Apache httpd,httpcfgs mean the configureation of app in httpd,and then restart httpd
echo_and_log "--> Adding additional Apache configurations"
httpdcfgs=( api.conf foxitcloud.crt foxitcloud.key )
for i in "${httpdcfgs[@]}"
do
	sudo \cp $SOURCE_ROOT/vagrant/svcconfig/httpd_config/$i /etc/httpd/conf.d/$i
	sudo sed -i "s/.vagrant./.$PC_ENV./g" /etc/httpd/conf.d/$i
done
echo_and_log "--> Restarting Apache server"
sudo service httpd restart

if ! grep -q "rm -rf /var/lib/mongo/mongod.lock" /etc/init.d/mongod; then
	POSITION="echo -n $\"Starting mongod: \""
	INSERT="rm -rf /var/lib/mongo/mongod.lock"
	sed -i "/$POSITION/i $INSERT" /etc/init.d/mongod
fi

#echo_and_log "--> Open the mongodb to the outside world, vagrant only"
#sudo sed -i "s/^bind_ip=127.0.0.1/#bind_ip=127.0.0.1/g" /etc/mongod.conf
#sudo service mongod restart

echo_and_log "--> Remove starting pcs-server from rc.local"
sed -i '/pcs-server/d' /etc/rc.local

